package com.huangfu.learning.javabase.java.reflect;

import jdk.nashorn.internal.ir.annotations.Ignore;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/7/13
 * Time: 16:08
 * Version: 1.0
 * Description:获取所有属性
 **/
public class GetAllClassInfo {

    public static void main(String[] args) {
        Class<?> clazz = null;
        try {
            clazz = Class.forName("com.huangfu.learning.javabase.java.reflect.Teacher");

            System.out.println("-------------↓↓↓↓↓构造函数↓↓↓↓↓-----------------");
            // 构造函数
            Constructor<?>[] constructors = clazz.getConstructors();
            for (Constructor constructor : constructors) {
                System.out.println("构造函数名：" + constructor.getName());
            }

            System.out.println("-------------↓↓↓↓↓父类↓↓↓↓↓-----------------");
            // 父类
            Class<?> supeprClass = clazz.getSuperclass();
            System.out.println("父类名：" + supeprClass.getName());

            System.out.println("-------------↓↓↓↓↓方法↓↓↓↓↓-----------------");
            // 方法
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                System.out.println("方法名:" + method.getName());
            }

            System.out.println("-------------↓↓↓↓↓↓↓属性↓↓↓↓-----------------");
            // 属性
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                System.out.println("属性名：" + field.getName());
                String fieldName = field.getName();
                // 权限修饰符
                int mo = field.getModifiers();
                String priv = Modifier.toString(mo);
                System.out.println("属性权限修饰符：" + priv);

                // 属性类型
                Class<?> type = field.getType();
                String typeName = type.getName();
                System.out.println("属性类型：" + typeName);

                System.out.println("最终属性信息：" + priv + " " + typeName + " " + fieldName);

            }

            System.out.println("-------------↓↓↓↓↓注解↓↓↓↓↓-----------------");
            // 注解
            Annotation[] annotations = clazz.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println("注解名：" + annotation.toString());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}


class Person {
    public void run() {
        System.out.println("Person run...");
    }
}

@Ignore
class Teacher extends Person {
    private String name;
    private int age;

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Teacher(String name) {
        this.name = name;
    }

    public Teacher(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void teach() {
        System.out.println("Teacher  teach");
    }
}
